
#ifndef _MEMBER_BMESH_H_
#define _MEMBER_BMESH_H_

#include <BVectorArray>
#include <BFaceArray>
#include <BColorArray>
#include <BCoordArray>
#include <BFaceArray>
#include <BTexture>
#include <BMaterial>
#include <BProgram>
#include <BMesh>
#include <BOctTree>
#include "ITriangleCallback.h"

#define mesh_member(mesh)		((member_BMesh*)((BMesh*)mesh)->_ptr)

namespace BWE
{
	struct FaceInfo
	{
		BFace	face;
		int		index = -1;
	};
	typedef BArray<FaceInfo>					FaceInfoArray;
	typedef BOctTree<FaceInfoArray>				FaceInfoOctTree;
	typedef BOctTree<FaceInfoArray>::Item		FaceInfoOctTreeItem;
	typedef BOctTree<FaceInfoArray>::ItemArray	FaceInfoOctTreeItemArray;

	class member_BMesh
	{
		friend class BMesh;

		struct Node
		{
			BSpace	space;
			BVector	center;
			int		index = -1;
		};
		typedef BArray<Node> NodeArray;

	public:
		member_BMesh(BMesh* mesh);
		~member_BMesh();

		void freshFaceOctTree();

		void buildTree(int startIndex, int endIndex);
		void overlapping(ITriangleCallback* callback, const BSpace& space);
		void overlapping(ITriangleCallback* callback, const BLine& line);
		void processNode(ITriangleCallback* callback, int index);

	public:
		BMesh*						boss;
		Policy						dataPolicy;
		BHolder<BVectorArray>		vertices;
		BHolder<BVectorArray>		normals;
		BHolder<BColorArray>		colors;
		BArray<BCoordArrayHolder>	coords;
		BHolder<BFaceArray>			faces;
		BHolder<FaceInfoOctTree>	faceOctTree;
		bool				dirtyTree;
		BSpace				localSpace;
		int					lastIndex;
		NodeArray			leafNodes;
		NodeArray			treeNodes;
	};
}

#endif